package com.example.study.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.entity.Page;
import com.example.study.entity.Result;
import com.example.study.mapper.UserMapper;
import com.example.study.pojo.Menu;
import com.example.study.pojo.User;
import com.example.study.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 喜欢多云的季节
 * @since 2022-04-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result select(Map<String, Object> params, Page page) {
        String  keyword = (String) params.get("keyword");
        Map<String, Object> map = new HashMap<String,Object>();
        //空判断直接返回所有数据
        if (page.getPageIndex() == null||page.getPageSize()==null) {
            page.setPageIndex(1);
            page.setPageSize(15);
        }
        //非空，按账号查询，和分页功能
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<User> userPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPageIndex(), page.getPageSize());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        if(!Strings.isEmpty(keyword)){
            userQueryWrapper.eq("account",keyword);
        }
        IPage<User> userIPage = userMapper.selectPage(userPage, userQueryWrapper);
        List<User> records = userIPage.getRecords();
        //存入返回数据
        map.put("users",records);
        map.put("page",page);
        map.put("totle",userMapper.selectCount(userQueryWrapper));
        return Result.ok(map);
    }
    @Transactional
    @Override
    public Result delete(List<String> params) {
        userMapper.deleteBatchIds(params);
        return Result.ok("删除成功");
    }
    @Transactional
    @Override
    public Result update(User user) {
        userMapper.updateById(user);
        return Result.ok("修改成功");
    }
    @Transactional
    @Override
    public Result insert(User user) {
        userMapper.insert(user);
        return Result.ok("新增成功");
    }

    @Override
    public void export( HttpServletResponse response) {
        List<User> users = userMapper.selectList(null);

        //ExcelType.HSSF 2003 office  | XSSF 2007
        ExportParams params = new ExportParams("员工表", "员工表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, users);
        ServletOutputStream outputStream=null;
        try {
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=user.xls");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=outputStream){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public List<Menu> getMenu(String account) {
        return userMapper.getMenu(account);
    }

    @Override
    public List<Map<String, Object>> menuTest(String account) {
        return userMapper.menuTest(account);
    }

}
